package com.vhly.classfile;

import java.util.Hashtable;
import java.util.Vector;

public class ConstantPool {

    private Vector<CPEntry> cp;
    private int vlen;

    public ConstantPool() {
        cp = new Vector<CPEntry>();
        vlen = cp.size();
    }

    public void removeAllElements() {
        cp.removeAllElements();
    }

    public CPEntry getCPEntryAtIndex(int i) {
        if (i < 0)
            return null;
        if (i >= cp.size()) {
            return null;
        } else {
            return (CPEntry) cp.elementAt(i);
        }
    }

    public void removeAt(int i) {
        cp.removeElementAt(i);
    }

    public int addCPEntry(CPEntry cpentry) {
        if (cp.contains(cpentry)) {
            return vlen;
        } else {
            vlen = cp.size();
            cp.addElement(cpentry);
            return vlen;
        }
    }

    public int size() {
        vlen = cp.size();
        return vlen;
    }

    public int indexOf(CPEntry entry){
        int ret = -1;
        if(entry != null){
            int size = cp.size();
            CPEntry e;
            int type = entry.getType();
            for(int i = 0;i<size;i++){
                e = cp.elementAt(i);
                if(type == e.getType()){
                    if(entry.equalsTo(e)){
                        ret = i;
                        break;
                    }
                }
            }
        }
        return ret;
    }
}